#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_mixer.h>


#include "types.h"
#include "defines.h"
#include "characters.h"

void Terminate(void)
{
	SDL_Quit();
#ifdef GP2X
	chdir("/usr/gp2x");
	execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
#endif
}

int main (int argc, char *argv[])
{
	int i;
	SDL_Surface *intro;

	/* Initialize SDL */
	if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
		fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
		exit (1);
	}
	atexit (Terminate);

	SDL_ShowCursor(SDL_DISABLE);

	/* Set 320x240 16-bits video mode */
	screen = SDL_SetVideoMode (320, 240, 16, SDL_SWSURFACE);
	if (screen == NULL) {
		fprintf (stderr, "Couldn't set 320x240x16 video mode: %s\n", SDL_GetError ());
		exit (2);
	}

	/* Check and open joystick device */
	if (SDL_NumJoysticks() > 0) {
		joy = SDL_JoystickOpen(0);
		if(!joy) {
			fprintf (stderr, "Couldn't open joystick 0: %s\n", SDL_GetError ());
		}
	}
	
	Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 1024);
	
	small_font = IMG_Load("images/small-font.bmp");
	large_font = IMG_Load("images/large-font.bmp");
	
	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
	
	SDL_Rect dstRect;
	dstRect.x = (320-254)/2;
	dstRect.y = (240-148)/2;
	dstRect.w = 254;
	dstRect.h = 148;
	
	intro = IMG_Load("images/intro.bmp");
	SDL_BlitSurface(intro, NULL, screen, &dstRect);
	SDL_Flip(screen);
	
	sleep(1);
	
	drawTextBox(CHARACTER_TAI, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz",LOAD_TEXTBOX); 
	
	CheckKeys();
	
	drawTextBox(CHARACTER_TAKATO, "Hello Tai whats up buddy?",LOAD_TEXTBOX); 
	
	CheckKeys();
	
	drawTextBox(CHARACTER_TAI, "Just Another quick test \nof how well this is working :P",LOAD_TEXTBOX); 
	
	CheckKeys();
	
	drawTextBox(CHARACTER_TAKATO, "and another for testing",LOAD_TEXTBOX); 
	
	//drawTextBox(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz");
	//drawText(large_font, screen, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz", 69, 178);

#ifdef GP2X
	/* Only use GP2X code here */
#endif

#ifdef WIN32
	/* Only use Windows code here */
#endif

	while (!done)
	{	
		SDL_Event event;

		/* Check for events */
		while (SDL_PollEvent (&event))
		{
            /*drawTextBox(CHARACTER_TAI, "No key pushed",LOAD_TEXTBOX);
            if(gUp) drawTextBox(CHARACTER_TAI, "Up pushed",LOAD_TEXTBOX); else drawTextBox(CHARACTER_TAI, "No key pushed",LOAD_TEXTBOX);
            if(gDown) drawTextBox(CHARACTER_TAI, "Down pushed",LOAD_TEXTBOX);
            if(gLeft) drawTextBox(CHARACTER_TAI, "Left pushed",LOAD_TEXTBOX);
            if(gRight) drawTextBox(CHARACTER_TAI, "Right pushed",LOAD_TEXTBOX);*/
            
			switch (event.type)
			{
				case SDL_KEYDOWN:
					/* if press Ctrl + C, terminate program */
					if ( (event.key.keysym.sym == SDLK_c) && (event.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) )
						done = 1;
						
					switch( event.key.keysym.sym )
                    {
                        case SDLK_UP: gUp = 1; break;
                        case SDLK_DOWN: gDown = 1; break;
                        case SDLK_LEFT: gLeft = 1; break;
                        case SDLK_RIGHT: gRight = 1; break;  
                        case SDLK_v: gButtonX = 1; break;   
                        default: break; 
                    }
					break;
				case SDL_KEYUP:
					switch( event.key.keysym.sym )
                    {
                        case SDLK_UP: gUp = 0; break;
                        case SDLK_DOWN: gDown = 0; break;
                        case SDLK_LEFT: gLeft = 0; break;
                        case SDLK_RIGHT: gRight = 0; break;
                        case SDLK_v: gButtonX = 0; break;      
                        default: break; 
                    }
					break;
				case SDL_JOYBUTTONDOWN:
					/* if press Start button, terminate program */
					if ( event.jbutton.button == VK_START )
						done = 1;
					switch( event.jbutton.button )
                    {
                        case VK_UP: gUp = 1; break;
                        case VK_DOWN: gDown = 1; break;
                        case VK_LEFT: gLeft = 1; break;
                        case VK_RIGHT: gRight = 1; break;
                        case VK_FX: gButtonX = 1; break;      
                        default: break; 
                    }
					break;
				case SDL_JOYBUTTONUP:
                    switch( event.jbutton.button )
                    {
                        case VK_UP: gUp = 0; break;
                        case VK_DOWN: gDown = 0; break;
                        case VK_LEFT: gLeft = 0; break;
                        case VK_RIGHT: gRight = 0; break;
                        case VK_FX: gButtonX = 0; break;      
                        default: break; 
                    }
					break;
				case SDL_QUIT:
					done = 1;
					break;
				default:
					break;
			}
		}    

		/* Processing */
	}

	return 0;
}
